<?php
$count = 0;
$res = $oldDB->query('SELECT * FROM '.$oldDB->tableName('share').' ORDER BY share_id ASC LIMIT '.$begin.','.$limit);
while($data = $oldDB->fetchArray($res))
{
	$count++;
	$share_id = (int)$data['share_id'];
	$share = $data;
	
	$share['title'] = addslashes($share['title']);
	$share['content'] = addslashes($share['content']);
	$share['status'] = 1;
	unset($share['user_name'],$share['index_img'],$share['is_index'],$share['server_code'],$share['cache_data']);
	switch($share['type'])
	{
		case 'ask':
			$share['type'] = 'bar';
			$share['rec_id'] = (int)FDB::resultFirst('SELECT tid FROM '.FDB::table('forum_thread').' WHERE share_id = '.$share_id);
		break;
		case 'ask_post':
			$share['type'] = 'bar_post';
			$share['rec_id'] = (int)FDB::resultFirst('SELECT tid FROM '.FDB::table('forum_post').' WHERE share_id = '.$share_id);
		break;
		case 'ershou':
			$share['type'] = 'default';
			$share['rec_id'] = 0;
		break;
	}
	
	if(empty($share['source']))
		$share['source'] = 'web';

	FDB::insert('share',$share,false,true);

	if(in_array($share['share_data'],array('goods','photo','goods_photo')))
	{
		$goods_count = 0;
		$photo_count = 0;
		$goods_prices = array();
		$photo_types = array();
		$is_rel_share = true;
		if(in_array($share['share_data'],array('goods','goods_photo')))
		{
			$res_goods = $oldDB->query('SELECT * FROM '.$oldDB->tableName('share_goods').' WHERE share_id = '.$share_id.' ORDER BY goods_id ASC');
			while($item = $oldDB->fetchArray($res_goods))
			{
				$sg = FDB::fetchFirst('SELECT id,price,base_id FROM '.FDB::table('share_goods').' WHERE id = '.$item['goods_id']);
				if($sg['id'] == 0)
				{
					$rel_goods = FDB::fetchFirst('SELECT * FROM '.FDB::table('goods').' WHERE keyid = \''.$item['goods_key'].'\'');
					if(!$rel_goods)
					{
						addUpdateGoods($item);
						$rel_goods = FDB::fetchFirst('SELECT * FROM '.FDB::table('goods').' WHERE keyid = \''.$item['goods_key'].'\'');
					}

					if($rel_goods)
					{
						if($item['base_id'] > 0)
						{
							$item['base_id'] = (int)FDB::resultFirst('SELECT id FROM '.FDB::table('share_goods').' WHERE id = \''.$item['base_id'].'\'');
							if($item['base_id'] == 0)
								$item['base_share'] = 0;
						}

						$goods = array();
						$goods['id'] = $item['goods_id'];
						$goods['goods_id'] = $rel_goods['id'];
						$goods['img_id'] = $rel_goods['img_id'];
						$goods['shop_id'] = $rel_goods['shop_id'];
						$goods['uid'] = $item['uid'];
						$goods['share_id'] = $share_id;
						$goods['price'] = $rel_goods['price'];
						$goods['sort'] = (int)$item['sort'];
						$goods['base_id'] = (int)$item['base_id'];
						$goods['base_share'] = (int)$item['base_share'];
						FDB::insert("share_goods",$goods);
						
						if($item['base_id'] == 0)
						{
							$is_rel_share = false;
							$goods_prices[] = $item['price'];
						}
					}
					else
						continue;
				}
				else
				{
					if($sg['base_id'] == 0)
					{
						$is_rel_share = false;
						$goods_prices[] = $sg['price'];
					}
				}
				$goods_count++;
			}
		}

		if(in_array($share['share_data'],array('photo','goods_photo')))
		{
			$res_photo = $oldDB->query('SELECT * FROM '.$oldDB->tableName('share_photo').' WHERE share_id = '.$share_id.' ORDER BY photo_id ASC');
			while($item = $oldDB->fetchArray($res_photo))
			{
				$sp = FDB::fetchFirst('SELECT id,type,base_id FROM '.FDB::table('share_photo').' WHERE id = '.$item['photo_id']);
				if($sp['id'] == 0)
				{
					if($item['base_id'] > 0)
					{
						$item['base_id'] = (int)FDB::resultFirst('SELECT id FROM '.FDB::table('share_photo').' WHERE id = \''.$item['base_id'].'\'');
						if($item['base_id'] == 0)
							$item['base_share'] = 0;
					}

					$photo = array();
					$photo['id'] = $item['photo_id'];
					$photo['uid'] = $item['uid'];
					$photo['share_id'] = $share_id;
					$photo['type'] = empty($item['type']) ? 'default' : $item['type'];
					$photo['sort'] = (int)$item['sort'];
					$photo['base_id'] = (int)$item['base_id'];
					$photo['base_share'] = (int)$item['base_share'];
					if((int)$item['base_id'] > 0)
					{
						$photo['img_id'] = (int)FDB::resultFirst('SELECT img_id FROM '.FDB::table('share_photo').' WHERE id = \''.$item['base_id'].'\'');
						FS('Image')->updateImageRel($photo['img_id']);
					}
					else
					{
						$photo_types[$item['type']] = 1;
						$is_rel_share = false;
						$img_id = addUpdateImage($item['img'],'share',$item['server_code'],(int)$item['img_width'],(int)$item['img_height']);
						if($img_id > 0)
							$photo['img_id'] = $img_id;
						else
							continue;
					}

					FDB::insert("share_photo",$photo);
				}
				else
				{
					if($sp['base_id'] == 0)
					{
						$is_rel_share = false;
						$photo_types[$sp['type']] = 1;
					}
				}
				$photo_count++;
			}
		}
		
		if($goods_count > 0 && $photo_count > 0)
			$share_data_type = 'goods_photo';
		elseif($goods_count > 0)
			$share_data_type = 'goods';
		elseif($photo_count > 0)
			$share_data_type = 'photo';
		else
			$share_data_type = 'default';
		
		$update_share = array();
		$update_share['share_data'] = $share_data_type;
		if($share_data_type == 'default' && $share['type'] == 'album_item')
		{
			FDB::delete('share_match','share_id = '.$share_id);
			FDB::delete('share_tags','share_id = '.$share_id);
			FDB::delete('share_category','share_id = '.$share_id);
			if($share['type'] == 'album_item')
			{
				FDB::delete('album_rec','share_id = '.$share_id);
				FDB::delete('album_share','share_id = '.$share_id);
				FDB::delete('album_share_index','share_id = '.$share_id);
				$update_share['type'] = 'default';
				$share['type'] = 'default';
			}
			$update_share['rec_id'] = 0;
		}
		FDB::update("share",$update_share,"share_id=".$share_id);
		
		if($goods_count > 0 || $photo_count > 0)
		{
			$imags_index = array();
			$imags_index['uid'] = $share['uid'];
			$imags_index['share_id'] = $share_id;
			$imags_index['collect_count'] = $share['collect_count'];
			$imags_index['status'] = 1;
			FDB::insert("share_user_images",$imags_index,false,true);
	
			if(!$is_rel_share)
			{
				$imags_index = array();
				$imags_index['uid'] = $share['uid'];
				$imags_index['share_id'] = $share_id;
				$imags_index['collect_count'] = $share['collect_count'];
				FDB::insert("share_images_index",$imags_index,false,true);
			}
			else
			{
				FDB::delete('share_match','share_id = '.$share_id);
				FDB::delete('share_category','share_id = '.$share_id);
				FDB::delete('share_tags','share_id = '.$share_id);
			}
		}

		if($goods_count > 0)
		{
			$goods_index = array();
			$goods_index['uid'] = $share['uid'];
			$goods_index['share_id'] = $share_id;
			$goods_index['status'] = 1;
			$goods_index['collect_count'] = $share['collect_count'];
			FDB::insert("share_user_goods",$goods_index,false,true);
			if(!$is_rel_share)
			{
				sort($goods_prices);
				$goods_index = array();
				$goods_index['uid'] = $share['uid'];
				$goods_index['share_id'] = $share_id;
				$goods_index['min_price'] = current($goods_prices);
				$goods_index['max_price'] = end($goods_prices);
				$goods_index['collect_count'] = $share['collect_count'];
				FDB::insert("share_goods_index",$goods_index,false,true);
			}
		}

		if($photo_count > 0)
		{
			$photo_index = array();
			$photo_index['uid'] = $share['uid'];
			$photo_index['share_id'] = $share_id;
			$photo_index['status'] = 1;
			$photo_index['collect_count'] = $share['collect_count'];
			FDB::insert("share_user_photo",$photo_index,false,true);
			
			if(isset($photo_types['dapei']))
				FDB::insert("share_user_dapei",$photo_index,false,true);
				
			if(isset($photo_types['look']))
				FDB::insert("share_user_look",$photo_index,false,true);

			if(!$is_rel_share)
			{
				unset($photo_index['status']);
				FDB::insert("share_photo_index",$photo_index,false,true);

				if(isset($photo_types['dapei']))
				{
					FDB::insert("share_dapei_index",$photo_index,false,true);
					if($goods_count > 0)
						FDB::insert("share_dapei_goods",$photo_index,false,true);
				}
					
				if(isset($photo_types['look']))
				{
					FDB::insert("share_look_index",$photo_index,false,true);
					if($goods_count > 0)
						FDB::insert("share_look_goods",$photo_index,false,true);
				}
			}
		}
	}
	
	if($share_data_type == 'default' && in_array($share['type'],array('default','comments','bar','bar_post')))
	{
		FDB::insert("share_text_index",array('share_id'=>$share_id,'uid'=>$share['uid']),false,true);
	}
	elseif($share['type'] == 'fav')
	{
		$parent_share = $oldDB->fetchFirst('SELECT * FROM '.$oldDB->tableName('share').' WHERE share_id = '.$share['parent_id']);
		if($parent_share)
		{
			$data = array();
			$data['share_id'] = $share['share_id'];
			$data['uid'] = $parent_share['uid'];
			$data['parent_id'] = $parent_share['share_id'];
			$data['cuid'] = $share['uid'];
			FDB::insert('fav_me',$data,false,true);
		}
	}

	FS('Share')->updateShareCache($share_id);
	FS('Share')->updateShareMatch($share_id);
	
	$cids = array();
	$res_cate = FDB::query('SELECT cate_id FROM '.FDB::table('share_category').'  
		WHERE share_id = '.$share_id);
	while($data = FDB::fetch($res_cate))
	{
		$cids[] = (int)$data['cate_id'];
	}
	FS('Share')->updateShareCate($share_id,$cids);
	showjsmessage("转换分享 ".$share_id." 成功",2);
	usleep(10);
}

if($count == 0 || $count < $limit)
	$args = array('table'=>$tableIndex + 1);
else
	$args = array('table'=>$tableIndex,'begin'=>$begin + $limit);

$total = $oldDB->resultFirst('SELECT COUNT(share_id) FROM '.$oldDB->tableName('share'));
$count = $total - $begin - $limit;
if($count < 0)
	$count = 0;

showjsmessage("还有 <b style='color:#f00;'>".$count."</b> 个分享待转换",2);

showjsmessage(U('Index/updatetable',$args),5);

function addUpdateGoods($data)
{
	global $oldDB;
	if(empty($data['goods_key']))
	{
		$data['goods_key'] = getGoodsKeyByUrl($data['url']);
		$oldDB->query("UPDATE ".$oldDB->tableName('share_goods')." SET goods_key = '".$data['goods_key']."' 
			WHERE goods_id = ".$data['goods_id']);
	}

	$goods_id = (int)FDB::resultFirst('SELECT id FROM '.FDB::table('goods').' WHERE keyid = \''.$data['goods_key'].'\'');
	if($goods_id == 0)
	{
		$goods = array();
		$goods['keyid'] = $data['goods_key'];
		if(!empty($data['img']))
		{
			$img_id = addUpdateImage($data['img'],'share',$data['server_code'],(int)$data['img_width'],(int)$data['img_height']);
			if($img_id > 0)
				$goods['img_id'] = $img_id;
			else
				return false;
		}
		else
			return false;

		$types = explode('_',$data['goods_key']);
		$goods['type'] = $types[0];
		$goods['shop_id'] = $data['shop_id'];
		$goods['cid'] = getCidByShareId($data['share_id']);
		$goods['name'] = addslashes($data['name']);
		$goods['url'] = addslashes($data['url']);
		$goods['taoke_url'] = addslashes($data['taoke_url']);
		$goods['price'] = $data['price'];
		$goods['delist_time'] = 0;
		$create_time = (int)$oldDB->resultFirst('SELECT create_time FROM '.$oldDB->tableName('share').' WHERE share_id = '.$data['share_id']);
		if($create_time == 0)
			$create_time = TIME_UTC;
		$goods['create_time'] = $create_time;

		$create_day = fToDate($goods['create_time'],'Y-m-d 00:00:00');
		$goods['create_day'] = str2Time($create_day);
		$goods_id = FDB::insert('goods',$goods,true);
		
		if($goods['cid'] == 0)
		{
			FDB::insert('goods_check',array('id'=>$goods_id));
		}

		$goods_match = array(
			'id' => $goods_id,
			'goods_name' => FS("Words")->segmentToUnicode($data['name']),
		);
		FDB::insert('goods_match',$goods_match,false,true);
	}
	
	return true;
}

function getGoodsKeyByUrl($url)
{
	if(strpos($url,'taobao') !== FALSE || strpos($url,'tmall') !== FALSE)
	{
		$id = 0;
		$parse = parse_url($url);
		if(isset($parse['query']))
		{
			parse_str($parse['query'],$params);
			if(isset($params['id']))
				$id = (float)$params['id'];
			elseif(isset($params['item_id']))
				$id = (float)$params['item_id'];
			elseif(isset($params['default_item_id']))
				$id = (float)$params['default_item_id'];
			elseif(isset($params['item_num_id']))
				$id = (float)$params['item_num_id'];
		}

		if($id > 0)
			return 'taobao_'.$id;
	}
	elseif(strpos($url,'paipai') !== FALSE)
	{
		$id = '';
		$parse = parse_url($url);
		if(isset($parse['path']))
		{
			$parse = explode('/',$parse['path']);
			$parse = end($parse);
			$parse = explode('-',$parse);
			$id = current($parse);
        }

		if(!empty($id))
			return 'paipai_'.$id;
	}
	elseif(strpos($url,'360buy') !== FALSE)
	{
		$id = '';
		$parse = parse_url($url);
		if(isset($parse['path']))
		{
			$parse = explode('/',$parse['path']);
			$parse = end($parse);
			$parse = explode('.',$parse);
			$id = current($parse);
        }

		if(!empty($id))
			return '360buy_'.$id;
	}
	elseif(strpos($url,'dangdang') !== FALSE)
	{
		$id = 0;
		$parse = parse_url($url);
		if(isset($parse['query']))
		{
            parse_str($parse['query'],$params);
			if(isset($params['product_id']))
				$id = (float)$params['product_id'];
            elseif(isset($params['id']))
                $id = (float)$params['id'];
        }

		if($id > 0)
			return 'dangdang_'.$id;
	}
	elseif(strpos($url,'vancl') !== FALSE)
	{
		$id = '';
		$parse = parse_url($url);
		if(isset($parse['path']))
		{

			$parse = explode('/',$parse['path']);
			$parse = end($parse);
			$parse = explode('.',$parse);
			$id = current($parse);
        }

		if(!empty($id))
			return 'vancl_'.$id;
	}
	elseif(strpos($url,'vjia') !== FALSE)
	{
		$id = '';
		$parse = parse_url($url);
		if(isset($parse['path']))

		{
			$url = $parse['scheme'].'://'.$parse['host'].$parse['path'];
			$parse = explode('/',$parse['path']);
			$parse = end($parse);
			$parse = explode('.',$parse);
			$id = current($parse);
			
        }
		if(!empty($id))
			return 'vjia_'.$id;
	}

	return 'default_'.md5($url);
}

function getCidByShareId($share_id)
{
	$list = array();
	global $oldDB;
	$res = $oldDB->query('SELECT cate_id FROM '.$oldDB->tableName('share_category').' WHERE share_id = '.$share_id);
	while($data = $oldDB->fetchArray($res))
	{
		if(isset($list[$data['cate_id']]))
			$list[$data['cate_id']]++;
		else
			$list[$data['cate_id']] = 1;
	}

	if(count($list) == 0)
		return 0;

	arsort($list);
	return key($list);
}
?>